{% extends 'course/base.html' %}
{% load staticfiles %}
{% block content %}
        <!-- Page Heading -->
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">
                    Exam
                </h1>
                <ol class="breadcrumb">
                    <li>
                        <i class="fa fa-university"></i> <a href="/courses">Courses</a>
                    </li>
                    <li>
                        <i class="fa fa-compass"></i> {{ course.title }}
                    </li>
                    <li>
                        <i class="fa fa-star"></i> <a href="/course/{{ course.id }}/exams">Exams</a>
                    </li>
                    <li class="active">
                        <i class="fa fa-star"></i> Exam #{{ exam.exam_num }}
                    </li>
                </ol>
            </div>
        </div>
        <!-- /.row -->

        <!-- Questions -->
        <div id="ajax_questions_table"
           name="ajax_questions_table">
           {% include "course/exam/question_table.html" %}
        </div>
        <!-- /.row -->
<script>
    //------------------------------------------------------------------------//
    //                           MULTIPLE CHOICE                              //
    //------------------------------------------------------------------------//

    function ajax_submit_mc_exam_answer(question_id, answer)
    {
        var url = {{ exam.exam_id }} + '/submit_mc_exam_answer';
        
        // create a FormData Object using your form dom element
        var data = new FormData();
        data.append('csrfmiddlewaretoken', '{{ csrf_token }}');
        data.append('question_id', question_id);
        data.append('answer', answer);
    
        jQuery.ajax({
            url: url,
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            type: 'POST',
            success: function(result){
                if (result.status != 'success') // Print errors.
                {
                    alert(result.message);
                }
            }
        });
    }

    //------------------------------------------------------------------------//
    //                               SUBMISSION                               //
    //------------------------------------------------------------------------//
    function ajax_submit_exam()
    {
        $('#submit_{{ exam.exam_id }}_btn').prop("disabled", true); // Lock button
                
        // create a FormData Object using your form dom element
        var data = new FormData();
        data.append('csrfmiddlewaretoken', '{{ csrf_token }}');
        
        jQuery.ajax({
            url: {{ exam.exam_id }} + '/submit_exam',
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            type: 'POST',
            success: function(result){
                if (result.status != 'success') // Print errors.
                {
                    alert(result.message);
                }
                else
                {
                    $('#submit_{{ exam.exam_id }}_btn').prop("disabled", false); // Unlock button
                    window.location = "../exams"
                }
            }
        });
    }
</script>
{% endblock content %}
